/*
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
*/
import javax.microedition.lcdui.TextField;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 *
 * @author Klaas Dons
 */
public class GetPlayer implements Runnable {
    private TextField textField;
    
    public GetPlayer(TextField textField){
        this.textField = textField;
    }
    
    /**
     * Geeft de spelersnaam terug die in de mobiele telefoon staat.
     */
    public void run(){
        String playerName = "";
        RecordStore rs = null;
        RecordEnumeration re = null;
        
        try {
            rs = RecordStore.openRecordStore("PlayerName", true);
            re = rs.enumerateRecords(null, null, false);
            
            if(re.hasNextElement()){
                byte[] raw = re.nextRecord();
                playerName = new String(raw);
            }
        } catch (RecordStoreException rse) {
        } finally {
            if (re != null){
                re.destroy();
            }
            
            if (rs != null){
                try {
                rs.closeRecordStore();
                } catch (RecordStoreException rse) {
                }
            }
        }
        
        this.textField.setString(playerName);
        /*
        FileConnection fc = null;
        InputStream is = null;
        
        try {
            fc = (FileConnection)Connector.open("file:///root1/player.txt", Connector.READ_WRITE);
            is = fc.openInputStream();
            
            if (!fc.exists()){
                fc.create();
                fc = (FileConnection)Connector.open("file:///root1/player.txt", Connector.READ_WRITE);
            }
            
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            
            byte[] data = new byte[512];
            int bytesRead = 0;
            if ((bytesRead = is.read(data)) > 0){
                baos.write(data, 0, bytesRead);

                this.textField.setString(baos.toString());
            }
        } catch (IOException e){
        } finally {
            if (is != null){
                try {
                    is.close();
                } catch (IOException e){
                }
            }
            
            if (fc != null){
                try {
                    fc.close();
                } catch (IOException e){
                }
            }
        }
        */
    }
}
